package com.rdb.mbean;

import com.rdb.entities.Messages;
import com.rdb.entities.Registers;
import com.rdb.entities.Users;
import com.rdb.mbean.util.JsfUtil;
import com.rdb.mbean.util.PaginationHelper;
import com.rdb.sbean.MessagesFacade;
import com.rdb.sbean.RegistersFacade;
import com.rdb.sbean.SubjectsFacade;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean(name = "registersController")
@SessionScoped
public class RegistersController implements Serializable {

    private Registers current;
    private DataModel items = null;
    @EJB
    private com.rdb.sbean.RegistersFacade ejbFacade;
    
    @EJB
    private com.rdb.sbean.SubjectsFacade ejbSubjectsFacade;
    
    @EJB
    private com.rdb.sbean.MessagesFacade ejbMessagesFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

    public RegistersController() {
    }

    public Registers getSelected() {
        if (current == null) {
            current = new Registers();
        }
        return current;
    }

    public SubjectsFacade getEjbSubjectsFacade() {
        return ejbSubjectsFacade;
    }

    public void setEjbSubjectsFacade(SubjectsFacade ejbSubjectsFacade) {
        this.ejbSubjectsFacade = ejbSubjectsFacade;
    }

    public MessagesFacade getEjbMessagesFacade() {
        return ejbMessagesFacade;
    }

    public void setEjbMessagesFacade(MessagesFacade ejbMessagesFacade) {
        this.ejbMessagesFacade = ejbMessagesFacade;
    }
    
    
    
    public void setSelected(Registers selected) {
        this.current = selected;
    }

    private RegistersFacade getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {
                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findAll());
                }
            };
        }
        return pagination;
    }

    public String prepareList() {
        recreateModel();
        return "List";
    }

    public String prepareList1() {
        recreateModel();
        return "/faces/admin/registers/List.xhtml";
    }
    

    public String prepareEdit() {
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            Messages messages = new Messages();
            messages.setExamId(null);
            messages.setStatus(Boolean.FALSE);
            messages.setRegisterId(current);
            messages.setUserId(current.getAssignFac());
            messages.setMessages("assign teachers to grade for the student " + current.getId().getStudentId().getEnRoll() + "of " + current.getId().getExamId().getExamId());
            ejbMessagesFacade.create(messages);
            JsfUtil.addSuccessMessage("Register recounting was successfully assigned.");
            return "List";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("bundle/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }
    
    public List<Users> getAllUserses(){
        List<Users> facList = new ArrayList<Users>();
        if(current.getId().getExamId().getSubjectId() == null){
            facList = (ejbSubjectsFacade.find(ejbSubjectsFacade.findAll().get(0).getId())).getUsersList();
        }else{
            facList = (ejbSubjectsFacade.find(current.getId().getExamId().getSubjectId().getId())).getUsersList();
        }
        return  facList;
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    @FacesConverter(forClass = Registers.class)
    public static class RegistersControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            RegistersController controller = (RegistersController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "registersController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Registers) {
                Registers o = (Registers) object;
                return getStringKey(o.getReId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Registers.class.getName());
            }
        }
    }
}
